package com.lry.basic.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.Data;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test{
    @Data
    @ColumnWidth(20)
    public static class TestVO {
        @ExcelProperty( value = "姓名",index = 0)
        private String name;
        @ExcelProperty( value = "年龄",index = 1)
        private int age;
        @ExcelProperty( value = "学校",index = 2)
        private String school;
    }


    ExcelWriter excelWriter;
    WriteSheet sheet1;
    WriteSheet sheet2;
    {
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(new File("D:/1.xlsx"),true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        excelWriter = EasyExcel.write(outputStream).build();

        sheet1 = EasyExcel.writerSheet(0, "sheet1").head(TestVO.class).build();
        sheet2 = EasyExcel.writerSheet(1, "sheet2").head(TestVO.class).build();
    }

    int i=0;

     /**
     * 多个sheet导入测试
     * @throws FileNotFoundException
     */
    @org.junit.jupiter.api.Test
    public void sheetImport() throws FileNotFoundException {
        if(i>=5){
            excelWriter.finish();
            return;
        }

        excelWriter.write(data(),sheet1);
        excelWriter.write(data(),sheet2);
        i++;

        sheetImport();
    }

    private  List<TestVO> data(){
        // 导出的数据
        List<TestVO> dataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            TestVO testVO = new TestVO();
            testVO.setAge(i + 20);
            testVO.setName("vo" + i);
            testVO.setSchool("school" + i);
            dataList.add(testVO);
        }
        return dataList;
    }
}
